<template>
  <div class="form-container">
    <el-form inline :model="form" label-width="80px">
      <el-form-item label="驾照类型" prop="typeName">
        <el-input size="default" v-model="form.typeName" placeholder="请输入驾照类型"></el-input>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <!-- <el-button type="primary" @click="handleReset(ruleForm as FormInstance)">重置</el-button> -->
      <el-button type="primary" v-if="btnFlag" @click="handleSearch">确定添加</el-button>
      <el-button type="primary" v-else @click="handleAdd">确定修改</el-button>
    </div>
  </div>
  <div class="wrap">
    <div>
      <el-radio-group v-model="radio1">
        <div class="radio-btn" @click.stop="deviceSelect(item)" v-for="item in driveTypeList" :key="item.id">
          <span @click.stop="btnRemove(String(item.id))">x</span>
          <div>
            <el-radio-button
              @click.prevent="radioSelect(item)"
              :label="item.typeName"
              :value="item.typeName + item.id"
            ></el-radio-button>
          </div>
        </div>
      </el-radio-group>
    </div>
  </div>
</template>
<script lang="tsx" setup name="deviceType">
import { onMounted, ref } from "vue";
import { DrivingType } from "@/api/interface/province/carManage";
import {
  getDriveTypeListAPI,
  DriveTypeDeleteAPI,
  addDriveTypeAPI,
  updateDriveTypeAPI
} from "@/api/modules/province/carManageAPI";
import { ElMessage, ElMessageBox } from "element-plus";
const radio1 = ref<string>("");
const radio2 = ref<string>("");
const btnFlag = ref<boolean>(true);
const driveTypeList = ref<DrivingType.Data[]>([
  {
    id: 1,
    typeName: "A类驾照",
    createBy: "string",
    createTime: "2019-08-24T14:15:22.123Z",
    updateBy: "string",
    updateTime: "2019-08-24T14:15:22.123Z"
  },
  {
    id: 1,
    typeName: "B类驾照",
    createBy: "string",
    createTime: "2019-08-24T14:15:22.123Z",
    updateBy: "string",
    updateTime: "2019-08-24T14:15:22.123Z"
  },
  {
    id: 1,
    typeName: "C1类驾照",
    createBy: "string",
    createTime: "2019-08-24T14:15:22.123Z",
    updateBy: "string",
    updateTime: "2019-08-24T14:15:22.123Z"
  },
  {
    id: 1,
    typeName: "C2类驾照",
    createBy: "string",
    createTime: "2019-08-24T14:15:22.123Z",
    updateBy: "string",
    updateTime: "2019-08-24T14:15:22.123Z"
  }
]);
let form = ref<{
  id?: number;
  typeName: string;
}>({
  typeName: ""
});
const btnRemove = (id: string): void => {
  ElMessageBox.confirm(`此操作将永久删除该驾照类型", 是否继续?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      // contents.value.splice(contents.value.indexOf(item), 1);
      deleteDriveType(id);
    })
    .catch(() => {
      ElMessage.info("已取消删除");
    });
};

const deviceSelect = (item: DrivingType.Data): void => {
  // btnFlag.value = false;
  // form.value.typeName = item.typeName;
  form.value.id = item.id as number;
};
const radioSelect = val => {
  btnFlag.value = false;
  if (radio2.value == "" || radio1.value == "") {
    radio2.value = val.typeName + val.id;
    radio1.value = val.typeName + val.id;
    form.value.typeName = val.typeName;
  } else {
    if (radio2.value != val.typeName + val.id) {
      radio1.value = val.typeName + val.id;
      radio2.value = val.typeName + val.id;
      form.value.typeName = val.typeName;
    } else {
      btnFlag.value = true;
      form.value.typeName = "";
      radio1.value = "";
    }
  }
  console.log(btnFlag.value);
};
const getDriveTypeList = async () => {
  const { code, data } = await getDriveTypeListAPI();
  if (code == "200" && data) {
    driveTypeList.value = data;
  }
};
const deleteDriveType = async (id: string) => {
  const { code } = await DriveTypeDeleteAPI(id);
  if (code == "200") {
    ElMessage.success("删除成功!");
    getDriveTypeList();
  }
};
const addDriveType = async (): Promise<void> => {
  const { code, msg } = await addDriveTypeAPI(form.value);
  if (code == "200") {
    ElMessage.success("添加成功!");
    getDriveTypeList();
    form.value = {
      typeName: ""
    };
    btnFlag.value = true;
  } else {
    ElMessage.error(msg);
  }
};
const updateDriveType = async (): Promise<void> => {
  const { code, msg } = await updateDriveTypeAPI({
    typeName: form.value.typeName,
    id: form.value.id as number
  });
  if (code == "200") {
    ElMessage.success("修改成功!");
    getDriveTypeList();
    form.value = {
      typeName: ""
    };
    btnFlag.value = true;
  } else {
    ElMessage.error(msg);
  }
};
const handleSearch = () => {
  if (form.value.typeName) {
    addDriveType();
  } else {
    ElMessage.error("请选择设施类型!");
  }
};
const handleAdd = () => {
  if (form.value.typeName) {
    updateDriveType();
  } else {
    ElMessage.error("请选择设施类型!");
  }
};
onMounted(() => {
  getDriveTypeList();
});
</script>
<style lang="scss" scoped>
.wrap {
  padding: 15px;
  background-color: white;
}
.form-container {
  padding: 10px 0;
  padding-right: 20px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
    ::v-deep .el-button > span {
      font-size: 13px;
    }
  }
  .el-form {
    display: inline-block;
    ::v-deep .el-form-item__label {
      font-size: 13px;
    }
    ::v-deep .el-input__inner {
      font-size: 13px;
    }
  }
  .form-line {
    .el-form-item {
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.btn {
  display: inline;
  float: right;
  margin-right: 10%;
}
.el-form-item {
  //   width: 320px;
  margin: 0;
}
.radio-btn {
  position: relative;
  display: inline-block;
  margin-right: 40px;
  background: whitesmoke;
  span {
    position: absolute;
    top: -8px;
    right: -8px;
    z-index: 1;
    display: inline-block;
    width: 16px;
    height: 16px;
    font-size: 12px;
    line-height: 14px;
    color: white;
    text-align: center;
    cursor: pointer;
    background-color: red;
    border-radius: 100%;
  }
  ::v-deep .el-radio-button__inner {
    width: 120px;
    padding: 13px 30px;
    font-size: 12px;
    background: lightgray;
  }
}
.select-container {
  box-sizing: border-box;
  height: 100px;
  padding-top: 30px;
  padding-left: 10px;

  // line-height: 100px;
  border: 1px solid #e2e2e2;
}
.title {
  margin-top: 20px;
  margin-bottom: 20px;
  font-size: 13px;
  color: gray;
}
</style>
